﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Drawing;
using DevExpress.Web.ASPxPivotGrid;


public partial class WebUserControl_ControlBaoCaoGiangCap : System.Web.UI.UserControl
{
    static string error = "";
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    static string usertype = "";
    static DataTable dataCap;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            dt = new ClassExecute.ClassExecute();
            usertype = dt.GetDataDepend(str, "GET_USERTYPE", CommandType.StoredProcedure, ref error, new SqlParameter("@UserName", Session["username"].ToString()));
            BindToThang();
            BindToDrop();
            if (usertype == "ad")
            {
                DropDownList1.SelectedValue = Session["sitecode"].ToString();
                DropDownList1.Enabled = true;
            }
            else
            {
                DropDownList1.SelectedValue = Session["sitecode"].ToString();
                DropDownList1.Enabled = false;
                
            }
        }
        else
        {
            try
            {
                Title();
                this.ASPxPivotGrid1.DataSource = dataCap;
                this.ASPxPivotGrid1.DataBind();
            }
            catch
            {
                return;
            }
        }
    }
    public void Title()
    {
        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["Baocao"]))
        { 
            ASPxPivotGrid1.Fields.Add("Baocao", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["Baocao"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["Baocao"].Caption = "Báo Cáo Giáng Cấp";
        }
        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["FromDate"]))
        {
            ASPxPivotGrid1.Fields.Add("FromDate", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["FromDate"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["FromDate"].Caption = "TTPP : " + DropDownList1.SelectedItem.Text;
        }
        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["Time"]))
        {
            ASPxPivotGrid1.Fields.Add("Time", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["Time"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["Time"].Caption = "Ngày giờ lấy báo cáo : " + DateTime.Now.ToShortDateString() + "  " + DateTime.Now.ToShortTimeString();
        }
    }
    void BindToDrop()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "GET_SITENAME_ALL", CommandType.StoredProcedure);
        DropDownList1.DataSource = db;
        DropDownList1.DataTextField = "sitename";
        DropDownList1.DataValueField = "Sitecode";
        DropDownList1.DataBind();
    }
    void BindToThang()
    { 
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getMonthList", CommandType.StoredProcedure);
        DropThang.DataSource = db;
        DropThang.DataTextField = "Names";
        DropThang.DataValueField = "Values";
        DropThang.DataBind();
    }
    protected void bt_baocao_Click(object sender, EventArgs e)
    {
        try
        {
            int check = 0;
            check = (check1.Items[0].Selected ? 1 : 0) + (check1.Items[1].Selected ? 1 : 0) + (check1.Items[2].Selected ? 1 : 0);
           
            if (check != 0)
            {

                ASPxPivotGrid1.Fields.Clear();
                Title();
                dt = new ClassExecute.ClassExecute();
                dataCap = dt.Get_Data_DataTable(str, ref error, "ls_getTitleProgress", CommandType.StoredProcedure,
                    new SqlParameter("@mMmYyyy", DropThang.SelectedValue.ToString()),
                    new SqlParameter("@siteCode", DropDownList1.SelectedValue.ToString()),
                    new SqlParameter("@userName", Session["username"].ToString()),
                    new SqlParameter("@duyTri", check1.Items[0].Selected ? 1 : 0),
                    new SqlParameter("@giangChuc", check1.Items[1].Selected ? 1 : 0),
                    new SqlParameter("@thangChuc", check1.Items[2].Selected ? 1 : 0));

                foreach (DataColumn col in dataCap.Columns)
                {

                    ASPxPivotGrid1.Fields.Add(col.ColumnName.ToString(), DevExpress.XtraPivotGrid.PivotArea.RowArea);

                }
                for (int j = 0; j < ASPxPivotGrid1.Fields.Count; j++)
                {
                    ASPxPivotGrid1.Fields[j].ValueFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                    ASPxPivotGrid1.Fields[j].ValueFormat.FormatString = "{0:###,###}";

                    ASPxPivotGrid1.Fields[j].CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                    ASPxPivotGrid1.Fields[j].CellFormat.FormatString = "{0:###,###}";
                }
                this.ASPxPivotGrid1.DataSource = dataCap;
                this.ASPxPivotGrid1.DataBind();
            }
            else
            {
                uscMsgBox1.AddMessage("Bạn phải chọn loại báo cáo trên checkbox", uscMsgBox.enmMessageType.Error);
            }
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            using (MemoryStream filestream = new MemoryStream())
            {

                string contentType = "application/ms-excel";
                string fileName = System.DateTime.Now.ToString("dd/MM/yyyy") + "_BaoCaoGiangCap.xls";
                ASPxPivotGridExporter1.ExportToXls(filestream);
                byte[] buffer = filestream.GetBuffer();
                Response.Clear();
                Response.Buffer = false;
                Response.AppendHeader("Content-Type", contentType);
                Response.AppendHeader("Content-Transfer-Encoding", "binary");
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
                Response.BinaryWrite(buffer);
                Response.End();
            }
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
}
